<?php
/**
 * A static class containing functions to calculate
 * a user's trust rating.
 *
 * @author    Kyle Levy
 * @author    Brent Sargent
 * @link      http://courses.washington.edu/ehealth2/spring_iteration/index.php
 * @copyright 2011 University of Washington, Tacoma. All Rights Reserved.
 */

/**
 * The maximum trust rating a user can have
 */
define("MAX_SCORE", 100);

/**
 * The path to the XML document containing active
 * metric modules.
 */
define("ACTIVE_MODULES", "../dao/modules.xml");

class Ratings
{
    private static $_modules = array();

    /**
     * Loads all metric modules from modules.xml
     * into the $modules array.
     * @access private
     * @static
     */
    private static function loadActiveModules()
    {
        $handle = simplexml_load_file(ACTIVE_MODULES);

        foreach($handle->module as $module => $value) {
            $mod = new Module();
            $mod->setUserId($user_id);
            $mod->setMetricName($module->metric_name);
            $modules[] = $mod;
        }
    }

    /**
     * Calculates a user's reputation rating given their performance
     * in each active metric module compared to the community average,
     * relative to the time that they have been registered with the community.
     * @param int $user_id
     * @access public
     * @static
     * @return int The user's reputation rating.
     */
    public static function calcualateRating($user_id)
    {
        loadActiveModules();
        $total_modules = count($_modules);
        $user_total    = 0;

        foreach($modules as $module) {
            $percentage = $module->getPercentage($user_id);
            $weighted   = $percentage * (1 / $total_modules);
            $user_total = $user_total + $weighted;
        }

        $current_rating = getCurrentRating($user_id);
        $new_rating     = $current_rating * $user_total;

        if($new_rating >  MAX_SCORE) {
            $new_rating = MAX_SCORE;
        }
        saveRating($new_rating);
    }

    /**
     * Saves a user's rating to _spring.user.rating
     * @param int rating the rating to be saved
     */
    private function saveRating($rating)
    {

    }

    /**
     * Gets the rating from dao/rating_dao/RatingDAO.fetchRating
     * @param int $user_id the user's ID
     */
    public function getCurrentRating($user_id)
    {

    }
}

/**
 * Represents a metric to be used in calculating a
 * user's trust rating.
 * @author    Kyle Levy
 * @author    Brent Sargent
 * @link      http://courses.washington.edu/ehealth2/spring_iteration/index.php
 * @copyright 2011 University of Washington, Tacoma. All Rights Reserved.
 */
class Module
{
    private $_metric_name;
    private $_user_metric;
    private $_community_metric;
    private $_user_id;

    /**
     * Setter function for $_metric_name
     * @param string $value the name of the metric
     */
    public function setMetricName($value)
    {
        $this->_metric_name = $value;
    }

    /**
     * Setter function for $_user_id
     * @param int $value the user's ID
     */
    public function setUserId($value)
    {
        $this->_user_id = $value;
    }

    /**
     * Module contructor
     * @param int $user_id the user's ID
     */
    public function __construct($user_id)
    {
        loadMetrics($user_id);
    }

    /**
     * Loads the metric data into $community_metric and
     * $user_metric from _spring.metrics
     * @param int $user_id the user's ID
     */
    private function loadMetrics($user_id)
    {

    }

    /**
     * Calculates and returns community_metric/user_metric
     * @return int the user's percentage in this area
     */
    public function getPercentage()
    {
        
    }
}
?>
